package com.yiqi.zhiyuan.feature.order.presenter;

import android.text.TextUtils;

import com.yiqi.zhiyuan.entity.dto.ComfirmTripDTO;
import com.yiqi.zhiyuan.entity.dto.TaskDTO;
import com.yiqi.zhiyuan.entity.vo.EndPushVO;
import com.yiqi.zhiyuan.entity.vo.OrderVO;
import com.yiqi.zhiyuan.feature.order.contract.OrderInfoContract;
import com.yiqi.zhiyuan.feature.order.dependencies.info.DaggerOrderInfoPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/25 11:16
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/25 11:16
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class OrderInfoPresenter implements OrderInfoContract.Presenter {

    private OrderInfoContract.View mView;

    @Inject
    OrderInfoContract.Model mModel;


    public OrderInfoPresenter(OrderInfoContract.View mView) {
        this.mView = mView;
        DaggerOrderInfoPresenterComponent.create().inject(this);
    }


    @Override
    public void endTask(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doEndTask(dto)
                .compose(mView.<EndPushVO>bindToLife())
                .subscribe(new Observer<EndPushVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(EndPushVO s) {
                        mView.dismissLoading();
                        mView.showEndTaskSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showEndTaskError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void startTask(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doStartTask(dto)
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showStartTaskSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showStartTaskError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void selectDriverOrderDetails(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doSelectDriverOrderDetails(dto)
                .compose(mView.<OrderVO>bindToLife())
                .subscribe(new Observer<OrderVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(OrderVO s) {
//                        mView.dismissLoading();
                        mView.showOrderDetailSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
//                        mView.dismissLoading();
                        mView.showOrderDetailError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
//                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void arriveAtLocation(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doArriveAtLocation(dto)
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showArriveAtLocationSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showStartTaskError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void passengersGettingOnBus(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doPassengersGettingOnBus(dto)
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showPassengersGettingOnBusSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showStartTaskError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void goNow(String orderNo, int useCarType) {
        if (TextUtils.isEmpty(orderNo)) {
            return;
        }
        TaskDTO dto = new TaskDTO();
        dto.setOrderNo(orderNo);
        dto.setUseCarType(useCarType);
        mModel.doGoNow(dto)
                .compose(mView.<OrderVO>bindToLife())
                .subscribe(new Observer<OrderVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(OrderVO s) {
                        mView.dismissLoading();
                        mView.showGoNowSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showStartTaskError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void doComfirmTrip(String orderNo, String others, String parking, String remark, String roadBridge, int useCarType) {

        ComfirmTripDTO dto=new ComfirmTripDTO();
        dto.setOrderNo(orderNo);
        dto.setOthers(others);
        dto.setParking(parking);
        dto.setRemark(remark);
        dto.setRoadBridge(roadBridge);
        dto.setUseCarType(useCarType);

        mModel.doComfirmTrip(dto)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                      mView.showComfirmTripSuccess();
                      mView.dismissLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showEndTaskError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });


    }

    @Override
    public void start() {

    }
}
